<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<script>
// 使用基础对象 Object 实现栈
class Stack {
  constructor() {
    this.items = {}
    this.length = 0
  }
  push(node) {
    this.items[this.length] = node;
    this.length++
  }
  pop() {
    if (this.isEmpty()) {
      return null;
    }
    this.length--
    const r = this.items[this.length]
    delete this.items[this.length]
    return r
  }
  // 获取栈顶节点
  peek() {
    if (this.isEmpty()) {
      return null
    }
    return this.items[this.length - 1]
  }
  isEmpty() {
    return this.length === 0
  }
  clear() {
    this.items = {}
    this.length = 0
  }
}

const stack = new Stack()

stack.push({a: 1})
stack.push({b: 2})
console.log(stack.peek())
console.log(stack)
console.log(stack.isEmpty())
stack.push({c: 3})
stack.push({d: 4})
stack.pop()
stack.items = {}
console.log(stack)
</script>


<body>
</body>
</html>